package com.lil.qmui.utils;

import android.text.TextUtils;
import android.util.Log;

import java.util.Locale;

/**
 * @author: Allen.
 * @date: 2018/7/25
 * @description:
 */

public class LogUtil {
    public static String logger = "logger";
    private static boolean isPrintLog = true; //是否打开日志打印

    public static boolean isDebug() {
        return isPrintLog;
    }

    public static void setDebug(boolean debug) {
        LogUtil.isPrintLog = debug;
    }


    /**
     * http请求日志用http做过滤标签
     */
    public static void printLogger(String logTxt, Object... args) {
        if (isPrintLog) {
            if (!TextUtils.isEmpty(logTxt)) {
                String lineHead;
                // 构造行首
                StackTraceElement trackTrace = new Throwable().getStackTrace()[2];
                lineHead = String.format(Locale.getDefault(), "%s:%s[%d]  ", trackTrace.getClassName(),
                        trackTrace.getMethodName(), trackTrace.getLineNumber());
                lineHead = Locale.getDefault() + trackTrace.getClassName() + ":" + trackTrace.getMethodName() + ":[" + trackTrace.getLineNumber() + "]";
                String lineContent = String.format(logTxt, args);
                Log.println(Log.VERBOSE, getShortHead("[HTTP]", lineHead), lineContent);

            }
        }
    }

    /**
     * 普通日志打印 LOG-INFO 做过滤标签
     */
    public static void print(String logTxt, Object... args) {
        if (isPrintLog) {
            if (!TextUtils.isEmpty(logTxt)) {
                String lineHead;
                // 构造行首
                StackTraceElement trackTrace = new Throwable().getStackTrace()[2];
                lineHead = String.format(Locale.getDefault(), "%s:%s[%d]  ", trackTrace.getClassName(),
                        trackTrace.getMethodName(), trackTrace.getLineNumber());
                String lineContent = String.format(logTxt, args);
                Log.println(Log.VERBOSE, getShortHead("[LOG-INFO]", lineHead), lineContent);

            }
        }


    }

    public static void log(String format, Object... args) {
        print(format, args);
    }

    public static void info(String info) {
        log(info);
    }

    public static void log(String str) {
        if (TextUtils.isEmpty(str)) {
            print("HTTP null");
        } else {
            print("HTTP " + str);
        }

    }

    /**
     * 日志的行首
     *
     * @param head
     * @return
     */
    private static String getShortHead(String tag, String head) {
        int index = head.lastIndexOf(46);
        String shortHead = null;

        if (index == -1) {
            shortHead = head;
        } else {
            shortHead = head.substring(index + 1);
        }
        shortHead = tag + shortHead;
        return shortHead;
    }


}
